package _98GUI多线程;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.*;

import javax.swing.*;

public class TestFrame extends JFrame{

	public TestFrame() {
	
		
		MyPanel p = new MyPanel();
		
		this.add(p);
		

	}
	

}

class Line
{
	int startX,startY,endX,endY;

	public Line(int startX, int startY, int endX, int endY) {
		super();
		this.startX = startX;
		this.startY = startY;
		this.endX = endX;
		this.endY = endY;
	}

	public Line() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
}

class MyPanel extends JPanel implements MouseListener
{
	int startX,startY,endX,endY;
	
	List<Line> lines = new ArrayList<>();
	
	
	public MyPanel() {
	
		addMouseListener(this);
	}
	
	@Override
	public void paint(Graphics g) {
//		super.paint(g);
		
		g.setColor(Color.RED);
		
//		g.drawLine(100, 100, 200, 100);
		for(Line l:lines)
		{
			g.drawLine(l.startX,l.startY,l.endX,l.endY);
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) 
	{
		startX = e.getX();
		startY = e.getY();
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
		endX = e.getX();
		endY = e.getY();
		
		Line l = new Line(startX, startY, endX, endY);
		lines.add(l);
		
		repaint();
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
